package com.andy.blog.dal.service.impl;

import com.andy.blog.common.exception.BlogServiceException;
import com.andy.blog.common.reponse.BlogResultCode;
import com.andy.blog.common.utils.DateUtils;
import com.andy.blog.common.utils.TransformUtils;
import com.andy.blog.dal.domain.SysLoginLogDO;
import com.andy.blog.dal.mapper.SysLoginLogMapper;
import com.andy.blog.dal.service.SysLoginLogDalService;
import com.andy.blog.dal.vo.SysLoginLogVO;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * Created by andy on 2023-04-05 11:38
 */
@Service
public class SysLoginLogDalServiceImpl implements SysLoginLogDalService {

    @Resource
    private SysLoginLogMapper loginLogMapper;

    /**
     * 新增系统登录日志
     *
     * @param sysLoginLogVO 访问日志对象
     */
    @Override
    public void insertLoginLog(SysLoginLogVO sysLoginLogVO) {
        SysLoginLogDO sysLoginLogDO = TransformUtils.transform(sysLoginLogVO, SysLoginLogDO.class);

        sysLoginLogDO.setLoginTime(DateUtils.getCurrentDate());

        try {
            loginLogMapper.insert(sysLoginLogDO);
        } catch (Exception e) {
            throw new BlogServiceException(BlogResultCode.BLOG_000100, e);
        }
    }

    /**
     * 查询系统登录日志集合
     *
     * @param sysLoginLogVO 访问日志对象
     * @return 登录记录集合
     */
    @Override
    public IPage<SysLoginLogDO> selectListByPage(SysLoginLogVO sysLoginLogVO) {
        LambdaQueryWrapper<SysLoginLogDO> lambdaQueryWrapper = buildQueryWrapper(sysLoginLogVO);

        IPage<SysLoginLogDO> page = new Page<>(sysLoginLogVO.getPageNum(), sysLoginLogVO.getPageSize());

        return loginLogMapper.selectPage(page, lambdaQueryWrapper);
    }

    /**
     * 构建查询条件
     */
    private LambdaQueryWrapper<SysLoginLogDO> buildQueryWrapper(SysLoginLogVO sysLoginLogVO) {
        LambdaQueryWrapper<SysLoginLogDO> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper
                .eq(StringUtils.isNotBlank(sysLoginLogVO.getIpAddress()), SysLoginLogDO::getIpAddress, sysLoginLogVO.getIpAddress())
                .eq(StringUtils.isNotBlank(sysLoginLogVO.getStatus()), SysLoginLogDO::getStatus, sysLoginLogVO.getStatus())
                .eq(StringUtils.isNotBlank(sysLoginLogVO.getClientType()), SysLoginLogDO::getClientType, sysLoginLogVO.getClientType())
                .like(StringUtils.isNotBlank(sysLoginLogVO.getUserName()), SysLoginLogDO::getUserName, sysLoginLogVO.getUserName())
                .ge(StringUtils.isNotBlank(sysLoginLogVO.getBeginTime()), SysLoginLogDO::getLoginTime, sysLoginLogVO.getBeginTime())
                .le(StringUtils.isNotBlank(sysLoginLogVO.getEndTime()), SysLoginLogDO::getLoginTime, sysLoginLogVO.getEndTime())
                .orderByDesc(SysLoginLogDO::getId);
        return lambdaQueryWrapper;
    }

    /**
     * 批量删除系统登录日志
     *
     * @param infoIds 需要删除的登录日志ID
     * @return 结果
     */
    @Override
    public int deleteByIds(List<Long> infoIds) {
        return loginLogMapper.deleteBatchIds(infoIds);
    }

    /**
     * 清空系统登录日志
     */
    @Override
    public void cleanLoginLog() {
        loginLogMapper.clean();
    }

    @Override
    public List<SysLoginLogDO> selectListByCondition(SysLoginLogVO sysLoginLogVO) {
        LambdaQueryWrapper<SysLoginLogDO> lambdaQueryWrapper = buildQueryWrapper(sysLoginLogVO);
        return loginLogMapper.selectList(lambdaQueryWrapper);
    }

    @Override
    public Long selectCount() {
        return loginLogMapper.selectCount(null);
    }
}
